Coverage Report

Created: 2026-11-19 22:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\cssh-rs\cssh-rs\cssh-rs-core\src\daemon\input.rs
Line
Count
Source
1
//! Conversion of daemon keystrokes into v1 messages.
2
3
use cssh_rs_protocol::v1::input::{InputEvent, SignalKind};
4
use cssh_rs_protocol::v1::keycode::{KeyCode, Modifiers};
5
use cssh_rs_protocol::v1::message::DaemonToClient;
6
use windows::Win32::System::Console::{
7
    KEY_EVENT_RECORD, LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED,
8
    SHIFT_PRESSED,
9
};
10
use windows::Win32::UI::Input::KeyboardAndMouse::{VIRTUAL_KEY, VK_CANCEL};
11
12
use crate::client::input_bytes::virtual_key_to_keycode;
13
14
/// Return the message that broadcasts a key-down event to the clients.
15
///
16
/// # Arguments
17
/// * `key` - The key-down event.
18
///
19
/// # Returns
20
/// A `Break` signal for Ctrl+Break, which has no input byte, otherwise the key.
21
6
pub fn key_event_to_message(key: &KEY_EVENT_RECORD) -> DaemonToClient {
22
6
    let virtual_key = VIRTUAL_KEY(key.wVirtualKeyCode);
23
6
    if virtual_key == VK_CANCEL {
24
1
        return DaemonToClient::Signal(SignalKind::Break);
25
5
    }
26
5
    let unicode_char = unsafe { key.uChar.UnicodeChar };
27
5
    let text = char::from_u32(u32::from(unicode_char))
28
5
        .filter(|character| return *character != '\0')
29
5
        .map(String::from);
30
5
    let code = match &text {
31
3
        Some(text) => KeyCode::Char(text.clone()),
32
2
        None => virtual_key_to_keycode(virtual_key).unwrap_or(KeyCode::Unknown),
33
    };
34
5
    return DaemonToClient::Input {
35
5
        event: InputEvent::Key {
36
5
            code,
37
5
            modifiers: modifiers_from_state(key.dwControlKeyState),
38
5
            text,
39
5
        },
40
5
    };
41
6
}
42
43
5
fn modifiers_from_state(state: u32) -> Modifiers {
44
5
    let mut modifiers = Modifiers::NONE;
45
5
    if state & SHIFT_PRESSED != 0 {
46
1
        modifiers = modifiers | Modifiers::SHIFT;
47
4
    }
48
5
    if state & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) != 0 {
49
1
        modifiers = modifiers | Modifiers::CTRL;
50
4
    }
51
5
    if state & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) != 0 {
52
1
        modifiers = modifiers | Modifiers::ALT;
53
4
    }
54
5
    return modifiers;
55
5
}
56
57
#[cfg(test)]
58
#[path = "../tests/daemon/test_input.rs"]
59
mod tests;